# -*- coding: utf-8 -*-
# Load some files for RunGAME

from json import load as jsonload
from json import dumps as jsondumps
from ConstVars import Path
import tkinter as tk
from tkinter import messagebox
from threading import Thread
from pygame.image import load as ImgLoad
from pygame import mixer
from pygame import font

# Initialize pygame mixer
mixer.init()
font.init()

def MessageFileNotFound(error):
    Frame = tk.Tk()
    Frame.withdraw()
    messagebox.showerror("RunGAME Error", error)
    Frame.destroy()

class StartSettings:
    ErrorBack = False
    try:
        settings = jsonload(open(Path +"/StartSettings.json", "r"))
    except FileNotFoundError as error:  # StartSettings.json File not found check
        ErrThd = Thread(target=MessageFileNotFound, args=(error,))
        ErrThd.start()
        ErrorBack = True
        exit()
    def save():
        with open(Path + "/StartSettings.json", "w") as f:
            f.write(jsondumps(StartSettings.settings))

class Files:
    ErrorBack = False
    try:
        # Load all the files
        class texture:
            class block:
                stone = ImgLoad(Path + "/texture/stone.png")
            class skin:
                player = ImgLoad(Path + "/texture/player_head.png")
            class background:
                dirt = ImgLoad(Path + "/texture/Dirt_background.png")
            class other:
                indicate = ImgLoad(Path + "/texture/indicate.png")
        class sound:
            class broken:
                stone = tuple(mixer.Sound(Path + f"/sound/broken/Stone_dig{i}.ogg") for i in range(1, 5))
            class place:
                stone = tuple(mixer.Sound(Path + f"/sound/place/Stone_dig{i}.ogg") for i in range(1, 5))
            class step:
                stone = tuple(mixer.Sound(Path + f"/sound/step/Stone_step{i}.ogg") for i in range(1, 6))
        class word:
            __WordList = dict((i, font.Font(Path + f"/font/Regular.otf", i)) for i in range(13, 21))
            info = __WordList[16]
            sound = __WordList[13]
            load = __WordList[20]
            cmd = __WordList[13]
    except FileNotFoundError as error:  # File not found check
        ErrThd = Thread(target=MessageFileNotFound, args=(error,))
        ErrThd.start()
        ErrorBack = True
        exit()
